package com.jyw.business.knowledge.controller;

import com.jyw.business.knowledge.domain.dto.*;
import com.jyw.business.knowledge.domain.vo.KlKnowledgeVo;
import com.jyw.business.knowledge.service.IKlKnowledgeService;
import com.jyw.common.annotation.Log;
import com.jyw.common.core.controller.BaseController;
import com.jyw.common.core.domain.CommonResult;
import com.jyw.common.core.page.TableDataInfo;
import com.jyw.common.enums.BusinessType;
import com.jyw.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author Created by ZhangCheng on 2024/01/15 (知识库申请)
 */
@RestController
@RequestMapping("/knowledge/apply")
@Api(tags = "知识库申请")
public class KlKnowledgeApplyController extends BaseController {

    @Resource
    private IKlKnowledgeService klKnowledgeService;

    /**
     * 查询知识库申请知识列表
     */
    @GetMapping("/applyList")
    @ApiOperation("查询知识库申请知识列表")
    public TableDataInfo<KlKnowledgeVo> applyList(KlKnowledgePageDto pageDto) {
        startPage();
        Boolean isAdmin = SecurityUtils.isAdmin();
        Long userId = getUserId();
        pageDto.setUserId(userId);
        pageDto.setIsAdmin(isAdmin);
        List<KlKnowledgeVo> list = klKnowledgeService.getApplyList(pageDto);
        return getDataTable(list);
    }


    /**
     * 新增知识库申请
     */
    @Log(title = "知识库申请", businessType = BusinessType.INSERT)
    @PostMapping(value = "add")
    @ApiOperation("新增知识库申请")
    public CommonResult add(@RequestBody @Valid KlKnowledgeAddDto addDto) {
        Long userId = getUserId();
        klKnowledgeService.add(addDto, userId);
        return CommonResult.success();
    }

    /**
     * 编辑知识库申请
     */
    @Log(title = "知识库申请", businessType = BusinessType.UPDATE)
    @PutMapping(value = "edit")
    @ApiOperation("编辑知识库申请")
    public CommonResult edit(@RequestBody @Valid KlKnowledgeEditDto editDto) {
        Long userId = getUserId();
        klKnowledgeService.edit(editDto,userId);
        return CommonResult.success();
    }

    /**
     * 通过知识库申请
     */
    @Log(title = "通过知识库申请", businessType = BusinessType.UPDATE)
    @PutMapping(value = "pass")
    @ApiOperation("通过知识库申请")
    public CommonResult pass(@RequestBody @Valid KlKnowledgePassDto passDto) {
        Long userId = getUserId();
        klKnowledgeService.pass(passDto, userId);
        return CommonResult.success();
    }

    /**
     * 驳回知识库申请
     */
    @Log(title = "驳回知识库申请", businessType = BusinessType.UPDATE)
    @PutMapping(value = "reject")
    @ApiOperation("驳回知识库申请")
    public CommonResult reject(@RequestBody @Valid KlKnowledgeRejectDto rejectDto) {
        Long userId = getUserId();
        klKnowledgeService.reject(rejectDto, userId);
        return CommonResult.success();
    }

    /**
     * 查询知识库申请详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("查询知识库申请详细信息")
    public CommonResult<KlKnowledgeVo> getInfo(@PathVariable("id") Long id) {
        return CommonResult.success(klKnowledgeService.getInfoById(id));
    }

    /**
     * 删除知识库-信息
     */
    @Log(title = "知识库-信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除知识库")
    public CommonResult remove(@PathVariable Long[] ids) {
        return CommonResult.success(klKnowledgeService.deleteKlKnowledgeByIds(ids));
    }
}
